﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using System.ComponentModel.Composition;

namespace VsSpeakerCompanion.UI
{
    [Export(typeof(IWidgetAnimationService))]
    internal class WidgetAnimationService : IWidgetAnimationService
    {
        List<IWidget> widgets = new List<IWidget>();
        DispatcherTimer timer = new DispatcherTimer();

        internal WidgetAnimationService()
        {
            // Set interval to 1.5 seconds
            timer.Interval = new TimeSpan(0, 0, 0, 0, 1500);
            timer.Tick += new EventHandler(timer_Tick);
        }

        /// <summary>
        /// See <see cref="IWidgetAnimationService"/> for more information
        /// </summary>
        /// <param name="widget"></param>
        public void Stop(IWidget widget)
        {
            if (widgets.Contains(widget))
            {
                widgets.Remove(widget);
            }
            if (widgets.Count == 0)
            {
                timer.Stop();
            }
        }

        /// <summary>
        /// See <see cref="IWidgetAnimationService"/> for more information
        /// </summary>
        /// <param name="widget"></param>
        public void Start(IWidget widget)
        {
            if (!widgets.Contains(widget))
            {
                widgets.Add(widget);
            }
            timer.Start();
        }

        private void timer_Tick(object source, EventArgs e)
        {
            foreach (IWidget widget in widgets)
            {
                FrameworkElement control = widget.VisualElement as FrameworkElement;
                control.BeginStoryboard(CreateStoryBoard(control));
            }
        }

        private Storyboard CreateStoryBoard(FrameworkElement control)
        {
            //<Storyboard x:Key="Flash">
            //    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)">
            //        <SplineDoubleKeyFrame KeyTime="00:00:00.2500000" Value="0.850"/>
            //        <SplineDoubleKeyFrame KeyTime="00:00:00.375" Value="1"/>
            //        <SplineDoubleKeyFrame KeyTime="00:00:00.4750000" Value="0.950"/>
            //        <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="1"/>
            //        <SplineDoubleKeyFrame KeyTime="00:00:00.7500000" Value="0.975"/>
            //        <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
            //    </DoubleAnimationUsingKeyFrames>
            //    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)">
            //        <SplineDoubleKeyFrame KeyTime="00:00:00.2500000" Value="0.850"/>
            //        <SplineDoubleKeyFrame KeyTime="00:00:00.375" Value="1"/>
            //        <SplineDoubleKeyFrame KeyTime="00:00:00.4750000" Value="0.950"/>
            //        <SplineDoubleKeyFrame KeyTime="00:00:00.6000000" Value="1"/>
            //        <SplineDoubleKeyFrame KeyTime="00:00:00.7500000" Value="0.975"/>
            //        <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
            //    </DoubleAnimationUsingKeyFrames>
            //</Storyboard>
            //<UserControl.RenderTransform>
            //    <ScaleTransform ScaleX="1" ScaleY="1" x:Name="ScaleTransform"/>
            //</UserControl.RenderTransform>

            Storyboard storyboard;

            //((UIElement)control.Content).RenderTransform = new TransformGroup { Children = new TransformCollection() { new ScaleTransform(1, 1), new TranslateTransform(0, 0) } };
            control.RenderTransform = new ScaleTransform(1, 1);
            control.RenderTransformOrigin = new Point(0.5, 0.5);

            storyboard = new Storyboard();
            DoubleAnimationUsingKeyFrames scaleX = new DoubleAnimationUsingKeyFrames();
            scaleX.KeyFrames.Add(new SplineDoubleKeyFrame(0.850, KeyTime.FromTimeSpan(new TimeSpan(2500000))));
            scaleX.KeyFrames.Add(new SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(new TimeSpan(3750000))));
            scaleX.KeyFrames.Add(new SplineDoubleKeyFrame(0.950, KeyTime.FromTimeSpan(new TimeSpan(4750000))));
            scaleX.KeyFrames.Add(new SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(new TimeSpan(6000000))));
            scaleX.KeyFrames.Add(new SplineDoubleKeyFrame(0.975, KeyTime.FromTimeSpan(new TimeSpan(7500000))));
            scaleX.KeyFrames.Add(new SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(new TimeSpan(10000000))));
            DoubleAnimationUsingKeyFrames scaleY = new DoubleAnimationUsingKeyFrames();
            scaleY.KeyFrames.Add(new SplineDoubleKeyFrame(0.850, KeyTime.FromTimeSpan(new TimeSpan(2500000))));
            scaleY.KeyFrames.Add(new SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(new TimeSpan(3750000))));
            scaleY.KeyFrames.Add(new SplineDoubleKeyFrame(0.950, KeyTime.FromTimeSpan(new TimeSpan(4750000))));
            scaleY.KeyFrames.Add(new SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(new TimeSpan(6000000))));
            scaleY.KeyFrames.Add(new SplineDoubleKeyFrame(0.975, KeyTime.FromTimeSpan(new TimeSpan(7500000))));
            scaleY.KeyFrames.Add(new SplineDoubleKeyFrame(1, KeyTime.FromTimeSpan(new TimeSpan(10000000))));

            storyboard.Children.Add(scaleX);
            Storyboard.SetTargetProperty(scaleX, new System.Windows.PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));

            storyboard.Children.Add(scaleY);
            Storyboard.SetTargetProperty(scaleY, new System.Windows.PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleY)"));

            return storyboard;
        }
    }
}
